<template>
  <uni-nav-bar title="绑定加注机" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
  <view class="package">{{price_name}}</view>
  <view class="package">
    <view class="space-between"><text>加注机</text>
    </view>
    <view class="price">
      <view v-for="(item,index) in fillList" :key='index' @click="addFill(item.id)"
        :class="checkbox2.indexOf(item.id)!==-1?'active':''">{{item.code}}</view>
    </view>
  </view>
  <view class="package">
    <view class="space-between"><text>司机价</text>
    </view>
    <view class="price">
      <view v-for="(item,index) in price" :key='index' @click="()=>moneyID=item.id"
        :class="moneyID==item.id?'active':''">{{item.price}}</view>
    </view>
  </view>
  <view class="add" @click="submit()">确定</view>
</template>

<script setup lang="ts">
  import { navBack } from '@/utils/navigator';
  import { ref } from 'vue';
  import { onLoad } from '@dcloudio/uni-app';
  import { showLoading, errorToast, successToast } from '@/utils/prompt';
  import { getCustomersFlii, getCustomersPrice } from '@/gql/administrator';
  import { toPublish } from '@mqtt';
  import { information } from '@/stores/administrator';
  const TeamInfo = information();
  /**
   * 绑定加注机
   * @property {Number} priceID 价格组ID
   * @property {String} checkbox2 选择的加注机ID
   * @property {Number} fillList 加注机列表
   * @property {Number} moneyID 司机价ID
   * @property {Number} price 司机价列表
   * @property {Number} price_name 价格组名称
   */
  const priceID = ref()
  const checkbox2 = ref([])
  const fillList = ref([])
  const moneyID = ref()
  const price = ref([])
  const price_name = ref('')
  onLoad((pearm) => {
    priceID.value = pearm.id
    price_name.value = pearm.name
  })
  //获取大客户司机价
  init()
  function init() {
    showLoading('');
    const payload = {
      query: getCustomersPrice,
      variables: { customers_id: Number(TeamInfo.KeyCustom.id) },
    };
    toPublish('ql/control/getCustomersPrice', payload, (obj : any) => {
      const { getCustomersPrice } = obj.data;
      price.value = getCustomersPrice
    });
  }
  //加注机列表
  FillAll()
  function FillAll() {
    showLoading()
    const payload = {
      query: getCustomersFlii,
      variables: {
        customers_id: Number(TeamInfo.KeyCustom.id),
        type: 'use'
      },
    };
    toPublish(
      'ql/control/getCustomersFlii',
      payload,
      (obj : any) => {
        uni.hideLoading();
        const { getCustomersFlii } = obj.data;
        fillList.value = getCustomersFlii
      }
    );
  }
  function addFill(id : number) {
    if (checkbox2.value.indexOf(id) === -1) {
      checkbox2.value.push(id)
    } else {
      checkbox2.value.map((item, index) => item === id ? checkbox2.value.splice(index) : '')
    }
  }
  /**
   * 提交价格组绑定的加注机
   * @property {Number} customers_id 大客户ID
   * @property {String} fill_ids 选择的加注机ID
   * @property {Number} price_group_id 价格组ID
   * @property {Number} price_id 司机价ID
   */
  function submit() {
    if (!checkbox2.value) {
      errorToast('请选择加注机')
      return
    }
    if (!moneyID.value) {
      errorToast('请选择司机价')
      return
    }
    showLoading('正在提交');
    const payload = {
      customers_id: Number(TeamInfo.KeyCustom.id),
      fill_ids: checkbox2.value.join(','),
      price_group_id: Number(priceID.value),
      price_id: Number(moneyID.value)
    };
    toPublish('control/addPriceGroupFill', payload, (obj : any) => {
      const { code, msg } = obj;
      if (code === 1) {
        successToast('提交成功');
        navBack()
      } else {
        errorToast(msg);
      }
    });
  }
</script>

<style scoped lang="less">
  .add {
    width: 480rpx;
    height: 100rpx;
    background: linear-gradient(-10deg, #2C96F9, #1B91FF);
    border-radius: 5px;
    text-align: center;
    line-height: 100rpx;
    color: #fff;
    margin: 40rpx auto 20rpx;
    font-size: 32rpx;
  }

  .price {
    width: 100%;
    display: flex;
    align-items: center;
    flex-wrap: wrap;

    view {
      min-width: 180rpx;
      height: 56rpx;
      padding: 0px 20rpx;
      box-sizing: border-box;
      text-align: center;
      line-height: 56rpx;
      border-radius: 5px;
      margin: 15rpx;
      position: relative;
      background: #ECF0F6;
      color: #333;
    }

    .active {
      background: #2694FB;
      color: #fff;
    }
  }
</style>